var ClubGroup_Chengyuanfenpei = Item.extend({
    _ccsFileName: res.ClubGroup_Chengyuanfenpei_json,
    _clubId: null,
    _tableView: null,
    _tableViewList: [],
    _groupData: [],
    _memberData: [],
    _partnerData: [],
    _curPartnerUid: null,
    _partnerUids: [],
    _curSelectUids:[],
    _curSpecUids:[],//朋友圈拥有者，和各组长
    _uid2ud:{}, //uid对应用户数据
    _selectCtrl:{
        'btn0': 0,
        'btn1': 1,
    },
    _curSelectCtrl: null,
    _dataSource: -1,

    initItem: function (clubId) {
        this._clubId = clubId;
        var clubInfo = ClubDataManager.getClubData(this._clubId);
        this._initSelectCtrl();
        this._initTableView();
    },
    _initClickListeners: function () {
        this._clickListeners = {
            'nodeSearch.btn_search': '_clickSearch',
            'btn_showList': '_showPartnerList',
            'panel_select.btn0': '_clickSelectCtrl',
            'panel_select.btn1': '_clickSelectCtrl',
            'btn_confirm': '_clickConfim',
        };
    },
    _initEventListeners: function () {
        this._eventListeners = {};
        this._eventListeners[ClubMsgCode.AllMemberInfo] = '_allMemberInfoHandler';
        this._eventListeners[ClubMsgCode.QueryClubGroup] = '_queryClubGroupHandler';
        this._eventListeners[ClubMsgCode.AddClubGroupUids] = '_modifyGroupDataHandler';
    },

    /**
     * 刷新界面显示
     */
    refreshView: function () {
        if (gameData.uid != ClubDataManager.getClubOwnerUid(this._clubId)){
            this._nodeList['panel_select'].setVisible(false);
            this._nodeList['panel_title'].setVisible(false);
            this._nodeList['nodeCurSelect'].setVisible(false);
            this._nodeList['cloneNode'].setVisible(false);
            this._nodeList['btn_showList'].setVisible(false);
            this._nodeList['nodeList'].setVisible(false);
            this._nodeList['nodeSearch'].setVisible(false);
            this._nodeList['nodeContent'].setVisible(false);
            this._nodeList['btn_confirm'].setVisible(false);
            this._nodeList['txt_notice'].setVisible(true);
            return;
        }
        else {
            this._nodeList['txt_notice'].setVisible(false);
        }

        this._groupData = [];
        this._memberData = [];
        this._partnerData = [];
        this._tableViewList = [];
        this._curSelectUids = [];
        this._uid2ud = [];
        this._curPartnerUid = null;
        // this._nodeList['card_num'].setString(gameData.numOfCards[1] + '张');
        //this._nodeList['nodeList'].setVisible(false);
        this._changeFilter();
    },
    //主动请求
    _changeFilter: function () {
        //1.请求分组列表QueryClubGroup
        clubMsgManager.requestGroups(this._clubId, true);
        //this._queryClubGroupHandler()
    },
    /**
     * 回调分组信息
     * @param data
     * @private
     */
    _queryClubGroupHandler: function(data){
        this._groupData = data.info.groups;
        this._dataSource = 0;
        //2.已成功后再请求成员列表allMemberInfo
        network.send(2103, {cmd: ClubMsgCode.AllMemberInfo, club_id: this._clubId, op: 2});
    },
    /**
     * 回调用户列表
     * @param data
     * @private
     */
    _allMemberInfoHandler:function(data){
        this._memberData = data.info.member;
        //成员数据
        //3.组装数据

        //合伙人列表数据
        var memberData = this._memberData;
        var groupData = this._groupData;
        var partnerData = this._partnerData;
        var groupLen = groupData.length;
        var memberLen = memberData.length;
        var partnerUids = this._partnerUids;

        var tmpLeaderData;
        for (var i = 0; i < groupLen; i++){
            tmpLeaderData = groupData[i];
            if (Number(groupData[i].op_uid) > 0){
                partnerData.push({uid:tmpLeaderData.op_uid, pos: tmpLeaderData.pos});
                partnerUids.push(tmpLeaderData.op_uid);
            }
        }

        //合伙人信息
        var partnerLen = partnerData.length;
        if (partnerLen == 0) {
            this._nodeList['nodeCurSelect'].setVisible(false);
            this._nodeList['cloneNode'].setVisible(false);
            return;
        }
        else {
            this._nodeList['nodeCurSelect'].setVisible(true);
        }

        var tmpMemberData;
        for (var i = 0; i < memberLen; i++){
            tmpMemberData = memberData[i];

            this._uid2ud[tmpMemberData.u] = tmpMemberData;

            if (partnerUids.indexOf(tmpMemberData.u) > -1){
                for (var j = 0; j < partnerLen; j++){
                    if (tmpMemberData.u == partnerData[j].uid){
                        partnerData[j].name = tmpMemberData.n;
                        partnerData[j].headimgurl = tmpMemberData.h;
                    }
                }
            }
        }

        //设置合伙人列表
        this._initPartnerList();
    },
    /**
     * 初始化TableView
     */
    _initTableView: function () {
        var nodeContent = this._nodeList['nodeContent'];
        var tableViewSize = nodeContent.getContentSize();
        this._tableView = new cc.TableView(this, cc.size(tableViewSize.width, tableViewSize.height));
        this._tableView.setDelegate(this);
        this._tableView.setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL);
        this._tableView.setVerticalFillOrder(cc.TABLEVIEW_FILL_TOPDOWN);
        this._tableView.setPosition(nodeContent.getPosition());
        nodeContent.getParent().addChild(this._tableView);
    },
    tableCellAtIndex: function (table, idx) {
        var self = this;
        var cell = table.dequeueCell();
        var node = null;
        if (cell == null) {
            cell = new cc.TableViewCell();
            node = addItem('ClubGroup_Chengyuanfenpei_Item', cell, this._tableViewList[idx], self);
            node.setName("itemCell");
        } else {
            node = cell.getChildByName("itemCell");
            node.setData(this._tableViewList[idx]);
        }
        return cell;
    },
    tableCellTouched: function (table, cell) {
    },
    tableCellSizeForIndex: function (table, idx) {
        return cc.size(1016, 80);
    },
    numberOfCellsInTableView: function (table) {
        return this._tableViewList.length;
    },
    /**
     * 初始化选择
     * @private
     */
    _initSelectCtrl: function(){
        this._curSelectCtrl = -1;
        this._clickSelectCtrl(this._nodeList['panel_select.btn0']);
    },
    _clickSelectCtrl: function(sender){
        var oldSelectCtrl = this._curSelectCtrl;
        var btnName = sender.getName();
        if (this._curSelectCtrl == this._selectCtrl[btnName]) return;
        this._curSelectCtrl = this._selectCtrl[btnName];
        if (btnName == 'btn0') {
            this._nodeList['panel_select.btn0.unselect'].setVisible(false);
            this._nodeList['panel_select.btn1.unselect'].setVisible(true);
            this._nodeList['panel_title.txt_02'].setVisible(true);
            this._nodeList['panel_title.txt_03'].setVisible(false);
        }
        else {
            this._nodeList['panel_select.btn0.unselect'].setVisible(true);
            this._nodeList['panel_select.btn1.unselect'].setVisible(false);
            this._nodeList['panel_title.txt_02'].setVisible(false);
            this._nodeList['panel_title.txt_03'].setVisible(true);
        }

        //清空搜索信息
        this._nodeList['nodeSearch.input_search'].setString('');

        //第一次不处理
        if (oldSelectCtrl > -1 && this._partnerUids.length > 0) {
            this._setGroupMemberList();
            this._tableView.reloadData();
        }
    },

    /**
     * 初始合伙人列表
     * @private
     */
    _initPartnerList: function () {
        var cloneNode = this._nodeList['cloneNode'];
        cloneNode.setVisible(false);

        if (this._curPartnerUid == null && this._dataSource == 0)
            this._initCurPartner(this._partnerData[0].uid);
        else
            this._initCurPartner(this._curPartnerUid);
    },
    /**
     * 生成合伙人列表
     * @private
     */
    _genPartnerList: function(){
        var self = this;

        var partnerList = this._nodeList['nodeList'];
        var cloneNode = this._nodeList['cloneNode'];
        cloneNode.setVisible(true);
        partnerList.removeAllChildren();
        var partnerData = this._partnerData;

        var singleParterData;
        for (var i = 0; i < partnerData.length; i++){
            singleParterData = partnerData[i];
            if (singleParterData.uid == this._curPartnerUid) continue;
            var custom_item = new ccui.Layout();
            custom_item.setContentSize(cc.size(342, 94));
            partnerList.pushBackCustomItem(custom_item);

            var cloneObj = duplicateNode(cloneNode);
            custom_item.addChild(cloneObj);
            cloneObj.setAnchorPoint(0, 0);
            cloneObj.setPosition(cc.p(16, 47));
            cloneObj.getChildByName('btn_cell').uid = singleParterData.uid;
            TouchUtil.setOnclickListener(cloneObj.getChildByName('btn_cell'), function (sender) {
                self._initCurPartner(sender.uid);
            });
            loadHeadImage(singleParterData.headimgurl, cloneObj.getChildByName('head'), true);//头像
            cloneObj.getChildByName('txt_name').setString(singleParterData.name);
            cloneObj.getChildByName('txt_id').setString(singleParterData.uid);
        }

        cloneNode.setVisible(false);
    },

    /**
     * 初始当前合伙人
     * @private
     */
    _initCurPartner: function(uid){
        //设置列表中的第一个为当前合伙人
        if (this._curPartnerUid == uid && this._dataSource == 0) return;
        this._curPartnerUid = uid;
        this._nodeList['nodeList'].setVisible(false);
        var curPartnerNode = this._nodeList['nodeCurSelect'];
        var curPartnerData = null;
        for (var i = 0; i < this._partnerData.length; i++){
            if (this._partnerData[i].uid == uid){
                curPartnerData = this._partnerData[i];
            }
        }
        if (curPartnerData != null) {
            loadHeadImage(curPartnerData.headimgurl, curPartnerNode.getChildByName('head'), true);//头像
            curPartnerNode.getChildByName('txt_name').setString(curPartnerData.name);
            curPartnerNode.getChildByName('txt_id').setString(curPartnerData.uid);
        }

        //清空搜索的内容
        this._nodeList['nodeSearch.input_search'].setString('');
        //对应的绑定信息和解绑信息
        this._setGroupMemberList();
        this._tableView.reloadData();
    },

    /**
     * 展示合伙人列表
     * @private
     */
    _showPartnerList: function () {
        var showListNode = this._nodeList['nodeList'];
        showListNode.setVisible(!showListNode.isVisible());
        if (showListNode.isVisible()){
            this._genPartnerList();
        }
    },

    _clickSearch: function () {
        this._setGroupMemberList();
        this._tableView.reloadData();
    },

    /**
     * 获得对应的组的数据
     * @private
     */
    _setGroupMemberList:function () {
        this._curSpecUids = [];
        var groupList = [];
        var curGroupId = 0;
        var memUids = [];
        var filterStr = this._nodeList['nodeSearch.input_search'].getString();
        var isCond = (filterStr == '' || filterStr.length == 0) ? false : true;

        if (this._curSelectCtrl == 0) curGroupId = 0;
        else if (this._curSelectCtrl == 1) curGroupId = this._curPartnerUid;
        var curGroupUids;
        for (var i = 0; i < this._groupData.length; i++){
            if (this._groupData[i].op_uid == curGroupId) {
                curGroupUids = this._groupData[i].uids;
                break;
            }
        }

        //如果是未分组的排除创建者和各组创建者
        if (curGroupId == 0){
            var ownId = ClubDataManager.getClubOwnerUid(this._clubId);
            var locationIdx = curGroupUids.indexOf(ownId)
            if (locationIdx > -1) {
                curGroupUids.splice(locationIdx, 1);
                this._curSpecUids.push(ownId);
            }
            for (var i = 0; i < this._partnerUids.length; i++){
                locationIdx = curGroupUids.indexOf(this._partnerUids[i]);
                if (locationIdx > -1) {
                    curGroupUids.splice(locationIdx, 1);
                    this._curSpecUids.push(this._partnerUids[i]);
                }
            }
        }

        var memberData = this._memberData;
        var memberLen = memberData.length;
        var locationIdx;
        for (var i = 0; i < memberLen; i++){
            locationIdx = curGroupUids.indexOf(memberData[i].u);
            if (locationIdx > -1 && isCond == false){
                groupList.push(memberData[i]);
                memberData[i].isSelected = false;
                memberData[i].loadObj = this;
            }
            else if (locationIdx > -1 && isCond == true && memberData[i].n.indexOf(filterStr) > -1){
                groupList.push(memberData[i]);
                memberData[i].isSelected = false;
                memberData[i].loadObj = this;
            }
        }
        this._curSelectUids = [];
        this._tableViewList = groupList;
    },
    /**
     * 绑定、解绑成员
     * @param uid
     * @param isSelectState
     */
    setCheckboxSelect:function (uid, isSelectState) {
        var locationIdx = this._curSelectUids.indexOf(uid);
        if (isSelectState == true){
            if (locationIdx > -1) return;
            else this._curSelectUids.push(uid);
        }
        else{
            if (locationIdx > -1) this._curSelectUids.splice(locationIdx, 1);
            else return;
        }
    },
    /**
     * 提交绑定和解绑
     * @private
     */
    _clickConfim: function () {
        if (this._curSelectUids.length == 0) return;

        var modifyGroupData = this._getModifyGroupData(this._curSelectCtrl);
        if (modifyGroupData == null) return;
        var set_group = {};
        set_group[modifyGroupData.addPos] = modifyGroupData.addUids;
        set_group[modifyGroupData.removePos] = modifyGroupData.removeUids;
        //发送修改数据
        network.send(2103, {
            cmd: ClubMsgCode.AddClubGroupUids,
            club_id: this._clubId,
            set_group: set_group,
            cv: 1
        });
        network.send(2103, {
            cmd: ClubMsgCode.AddLog,
            club_id: this._clubId,
            opInfo: modifyGroupData.opInfo
        });
    },
    /**
     * 获得修改的分组的信息
     * @private
     */
    _getModifyGroupData: function (ctrlOp) {
        var retData = {addPos: -1, addUids:[], removePos: -1, removeUids: []};
        var singGroupData;
        if (ctrlOp == 0){
            retData.removePos = 0;
            for (var i = 0; i < this._groupData.length; i++) {
                singGroupData = this._groupData[i];
                if (singGroupData.op_uid == this._curPartnerUid){
                    retData.addPos = singGroupData.pos;
                    retData.addUids = singGroupData.uids;
                }
                if (singGroupData.op_uid == 0){
                    retData.removeUids = singGroupData.uids;
                }
            }
        }
        else if (ctrlOp == 1){
            retData.addPos = 0;
            for (var i = 0; i < this._groupData.length; i++) {
                singGroupData = this._groupData[i];
                if (singGroupData.op_uid == 0){
                    retData.addUids = singGroupData.uids;
                }
                if (singGroupData.op_uid == this._curPartnerUid){
                    retData.removeUids = singGroupData.uids;
                    retData.removePos = singGroupData.pos;
                }
            }

        }
        else return null;

        //修改数据
        for (var i = 0; i < this._curSelectUids.length; i++){
            var li1 = retData.addUids.indexOf(this._curSelectUids[i]);
            var li2 = retData.removeUids.indexOf(this._curSelectUids[i]);
            if (li1 < 0) retData.addUids.push(this._curSelectUids[i]);
            if (li2 > -1) retData.removeUids.splice(li2, 1);
        }
        if (ctrlOp == 0){
            for (var i = 0; i < this._curSpecUids.length; i++){
                retData.removeUids.push(this._curSpecUids[i]);
            }

        }

        var opedUD = [];
        for (var i = 0; i < this._curSelectUids.length; i++){
            opedUD.push({uid: this._curSelectUids[i], name: this._uid2ud[this._curSelectUids[i]].n});
        }

        retData.opInfo = {op: ctrlOp, uds:opedUD, opUid: this._curPartnerUid,
            opName: this._uid2ud[this._curPartnerUid].n};

        return retData;
    },

    /**
     * 修改组数据回调
     * @param data
     * @private
     */
    _modifyGroupDataHandler: function(data){
        showToast('操作成功');
        this._groupData = [];
        this._memberData = [];
        this._partnerData = [];
        this._tableViewList = [];
        this._curSelectUids = [];
        this._groupData = data.info.groups;
        this._dataSource = 1;
        network.send(2103, {cmd: ClubMsgCode.AllMemberInfo, club_id: this._clubId, op: 2});
    },

});